main.js ➔ define   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 126

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 126
rs 8.2857
c 0
b 0
f 0
cc 1
nc 1
nop 3

11 Functions

Rating   Name   Duplication   Size   Complexity  
A main.js ➔ ... ➔ .saveTab 0 13 1
A main.js ➔ ... ➔ .initialize 0 3 1
A main.js ➔ ... ➔ .setData 0 3 1
A main.js ➔ ... ➔ .afterSave 0 4 1
A main.js ➔ ... ➔ .delete 0 8 1
A main.js ➔ ... ➔ .save 0 7 1
A main.js ➔ ... ➔ .loadingSave 0 3 1
A main.js ➔ ... ➔ .bindCustomEvents 0 6 1
B main.js ➔ ... ➔ .header 0 38 1
A main.js ➔ ... ➔ .loadComponentData 0 14 2
A main.js ➔ ... ➔ .enableSave 0 3 1

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
/*
2
 * This file is part of Sulu.
3
 *
4
 * (c) MASSIVE ART WebServices GmbH
5
 *
6
 * This source file is subject to the MIT license that is bundled
7
 * with this source code in the file LICENSE.
8
 */
9
10
define([
11
    'jquery',
12
    'services/sulucomment/thread-manager',
13
    'services/sulucomment/thread-router'
14
], function($, Manager, Router) {
15
16
    'use strict';
17
18
    return {
19
20
        defaults: {
21
            translations: {
22
                headline: 'sulu_comment.threads'
23
            }
24
        },
25
26
        header: function() {
27
            var buttons = {
28
                save: {},
29
                edit: {
30
                    options: {
31
                        dropdownItems: {
32
                            delete: {
33
                                options: {
34
                                    callback: this.delete.bind(this)
35
                                }
36
                            }
37
                        }
38
                    }
39
                }
40
            };
41
42
            return {
43
                title: function() {
44
                    return this.translations.headline;
45
                }.bind(this),
46
47
                tabs: {
48
                    url: '/admin/content-navigations?alias=threads',
49
                    options: {
50
                        data: function() {
51
                            return this.sandbox.util.extend(false, {}, this.data);
52
                        }.bind(this)
53
                    },
54
                    componentOptions: {
55
                        values: this.data
56
                    }
57
                },
58
59
                toolbar: {
60
                    buttons: buttons
61
                }
62
            };
63
        },
64
65
        loadComponentData: function() {
66
            var promise = $.Deferred();
67
68
            if (!this.options.id) {
69
                promise.resolve({});
70
71
                return promise;
72
            }
73
            Manager.load(this.options.id).done(function(data) {
74
                promise.resolve(data);
75
            });
76
77
            return promise;
78
        },
79
80
        initialize: function() {
81
            this.bindCustomEvents();
82
        },
83
84
        bindCustomEvents: function() {
85
            this.sandbox.on('sulu.header.back', Router.toList);
86
            this.sandbox.on('sulu.tab.dirty', this.enableSave.bind(this));
87
            this.sandbox.on('sulu.toolbar.save', this.save.bind(this));
88
            this.sandbox.on('sulu.tab.data-changed', this.setData.bind(this));
89
        },
90
91
        delete: function() {
92
            this.sandbox.emit('sulu.header.toolbar.item.loading', 'edit');
93
94
            Manager.delete(this.data.id).done(function() {
95
                this.sandbox.emit('sulu.header.toolbar.item.enable', 'edit', false);
96
                Router.toList();
97
            }.bind(this));
98
        },
99
100
        save: function(action) {
101
            this.loadingSave();
102
103
            this.saveTab().then(function(data) {
104
                this.afterSave(action, data);
105
            }.bind(this));
106
        },
107
108
        setData: function(data) {
109
            this.data = data;
110
        },
111
112
        saveTab: function() {
113
            var promise = $.Deferred();
114
115
            this.sandbox.once('sulu.tab.saved', function(savedData) {
116
                this.setData(savedData);
117
118
                promise.resolve(savedData);
119
            }.bind(this));
120
121
            this.sandbox.emit('sulu.tab.save');
122
123
            return promise;
124
        },
125
126
        enableSave: function() {
127
            this.sandbox.emit('sulu.header.toolbar.item.enable', 'save', false);
128
        },
129
130
        loadingSave: function() {
131
            this.sandbox.emit('sulu.header.toolbar.item.loading', 'save');
132
        },
133
134
        afterSave: function(action, data) {
135
            this.sandbox.emit('sulu.header.toolbar.item.disable', 'save', true);
136
            this.sandbox.emit('sulu.header.saved', data);
137
        }
138
    };
139
});
140